package tp.pr1;

import java.util.StringTokenizer;

public class Parser
{
	public Parser()
	{

	}
private VerbCommands parseFirstWord(String firstWord) {
	VerbCommands verb=VerbCommands.UNKNOWN;
	VerbCommands[] validCommands = VerbCommands.values();
	for (int i = 0; i < validCommands.length; i++)
	{
		if (validCommands[i].toString().equalsIgnoreCase(firstWord))
			verb = validCommands[i];
	}
	
	return verb;
}
private Directions parseSecondWord(String secondWord) {
	Directions dir=Directions.UNKNOWN;
	Directions[] validDirections = Directions.values();
	for (int i = 0; i < validDirections.length; i++)
	{
		if (validDirections[i].toString().equalsIgnoreCase(secondWord))
			dir = validDirections[i];
	}
	
	return dir;
}
public Command nextCommand(String line)
	{
		Command command=null;
		String firstCommand = null;
		String secondCommand = null;
		
		StringTokenizer tkn = new StringTokenizer(line);
		int numberOfInstructions=tkn.countTokens();
		
		if ((numberOfInstructions>2)||(numberOfInstructions==0)) 
			command=new Command();
		else {
			firstCommand = tkn.nextToken();
		
			if (numberOfInstructions==2)
				secondCommand = tkn.nextToken();
		
			command = new Command(parseFirstWord(firstCommand));//,parseSecondWord(secondCommand));
			command.setDirection(parseSecondWord(secondCommand));

		}
		return command;
	}
public String getHelp()
	{
		return ("You are lost. You are alone. You wander around.\n"+
				"Your command words are:\n" +
				"  HELP\n"+"  GO { NORTH | SOUTH | EAST | WEST }\n"+"  QUIT\n");
	}

}
